const express = require('express');
const router = express.Router();
const db = require('../db/db');

router.post('/api/list', (req, res) => {
    db.Question.Model.find()
        .populate({
            path: 'author',
            select: 'name'
        })
        .exec()
        .then(result => {
            console.log('获取首页列表数据', result)
            res.send({ code: 0, message: '获取列表成功', data: result })
        }).catch(err => {
            console.log('获取首页列表数据', err)
        })
})

router.post('/api/questionDetails', (req, res) => {
    db.Question.Model.findById(req.body.questionId)
        .populate('author', 'name')
        .populate({
            path: 'answers',
            populate: {
                path: 'author',
                select: 'name'
            }
        })
        .exec()
        .then(result => {
            console.log('获取详情成功', result)
            res.send({ code: 0, message: '获取成功', data: result });
        })
        .catch(err => {
            console.log('获取详情失败', err)
        })
})

module.exports = router;